package com.site.servlet;
/**
 * FileName: MasterServlet.java
 * 系统操作servlet层
 *
 * @author  王威
 * @Date    2021/12/1
 * @version 1.00
 **/
import com.alibaba.fastjson.JSON;
import com.site.pojo.Employee;
import com.site.service.EmployeeService;
import com.site.service.impl.EmployeeServiceImpl;
import com.sun.org.apache.regexp.internal.RE;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/master.do")
public class MasterServlet extends HttpServlet {
    private EmployeeService employeeService = new EmployeeServiceImpl();
    PrintWriter out = null;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        String opr = req.getParameter("opr");
        try {
            out = resp.getWriter();
            switch (opr){
                case "checkuser":
                    out.write(JSON.toJSONString(checkname(req,resp)));
                    break;
                case "login":
                    out.write(JSON.toJSONString(login(req,resp)));
                    break;
                case "clearuser":
                    out.write(JSON.toJSONString(clearuser(req,resp)));
                    break;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            out.flush();
            out.close();
        }
    }
    /*检查用户是否存在*/
    public Employee checkname(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        String user = req.getParameter("user");
        Employee employee = new Employee();
        employee.setEname(user);
        return employeeService.selectByname(employee);
    }
    /*用户验证登录*/
    public Employee login(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        String user = req.getParameter("user");
        String password = req.getParameter("password");
        Employee employee = new Employee();
        employee.setEname(user);
        employee.setEpassword(password);
        HttpSession session = req.getSession();
        session.setAttribute("employee",employeeService.login(employee));
        return employeeService.login(employee);
    }

    /*注销当前账户*/
    public boolean clearuser(HttpServletRequest req, HttpServletResponse resp){
        HttpSession session = req.getSession();
        session.removeAttribute("employee");
        Employee employee = (Employee) session.getAttribute("employee");
        return employee==null?true:false;
    }

}
